package com.example.chatplatform.mapper;

import com.example.chatplatform.model.dto.AddCronyDTO;
import com.example.chatplatform.model.vo.CronyInformVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CronyMapper {

    @Select("select user_crony_id from user_crony where user_id = #{userId} and crony_id = #{cronyId}")
    Integer isCrony1(Integer userId, Integer cronyId);

    @Select("select found_status from found_crony where user_id = #{userId} and crony_id = #{cronyId}")
    Integer isCrony2(Integer userId, Integer cronyId);

    @Select("select found_crony_id from found_crony where user_id = #{userId} and crony_id = #{cronyId}")
    Integer foundCrony(AddCronyDTO addCrony);

    @Update("update found_crony set user_message = #{userMessage}, found_time = #{foundTime}, found_status = #{foundStatus} where user_id = #{userId} and crony_id = #{cronyId}")
    Integer updateCrony(AddCronyDTO addCrony);

    @Insert("insert into found_crony(user_id, crony_id, user_message, found_time, found_status) " +
            "values (#{userId}, #{cronyId}, #{userMessage}, #{foundTime}, #{foundStatus})")
    Integer addCrony(AddCronyDTO addCrony);

    @Select("select found_crony_id, found_crony.user_id, user_avatar, user_name, user_message, found_time, found_status from found_crony " +
            "join user on user.user_id = found_crony.user_id " +
            "where crony_id = #{userId}")
    @Results({
            @Result(property = "foundCronyId", column = "found_crony_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "userAvatar", column = "user_avatar"),
            @Result(property = "userName", column = "user_name"),
            @Result(property = "userMessage", column = "user_message"),
            @Result(property = "foundTime", column = "found_time"),
            @Result(property = "foundStatus", column = "found_status")
    })
    List<CronyInformVO> getCronyInform(Integer userId);

    @Update("update found_crony set found_status = 1 where user_id = #{userId} and crony_id = #{cronyId}")
    Integer agreeCrony(Integer cronyId, Integer userId);

    @Insert({
            "INSERT INTO user_crony(user_id, crony_id, user_crony_time)" +
            "VALUES (#{userId}, #{cronyId}, #{formattedDateTime})," +
            "(#{cronyId}, #{userId}, #{formattedDateTime})"
    })
    Integer addUserCrony(Integer cronyId, Integer userId, String formattedDateTime);

    @Update("update found_crony set found_status = 2 where user_id = #{userId} and crony_id = #{cronyId}")
    Integer refusalCrony(Integer cronyId, Integer userId);

    @Delete("""
    delete from user_crony 
    where (user_id = #{userId} and crony_id = #{cronyId})
       or (user_id = #{cronyId} and crony_id = #{userId})
    """)
    int deleteCrony(Integer userId, Integer cronyId);

    @Delete("""
    delete from crony_message 
    where (messaging_id = #{userId} and receive_id = #{cronyId})
       or (messaging_id = #{cronyId} and receive_id = #{userId})
    """)
    int deleteCronyMessage(Integer userId, Integer cronyId);

    @Delete("""
    delete from found_crony 
    where (user_id = #{userId} and crony_id = #{cronyId})
       or (user_id = #{cronyId} and crony_id = #{userId})
    """)
    int deleteFoundCrony(Integer userId, Integer cronyId);
}
